﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using WeShare.WeiXin.Common;
using WeShare.WeiXin.Common.EnumKey;
using WeShare.WeiXin.Mp.JsSdk.Entity;

namespace WeShare.WeiXin.Mp.JsSdk
{
    /// <summary>
    /// jssdk获取config参数
    /// </summary>
    public class JsApi
    {
        public static WxJsParam GetJsParam(string url, bool debug = false, string appid = "")
        {
            var config = OpenApi.GetConfig(appid, PlatformType.Mp);
            return new WxJsParam(config.AppId, url, debug);
        }
        /// <summary>
        /// 获取选择卡券列表的参数
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="cardType"></param>
        /// <param name="cardId"></param>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static ChooseCardParam GetChooseCardParam(string shopId = "", CardType? cardType = null, string cardId = "", string appid = "")
        {
            var config = OpenApi.GetConfig(appid, PlatformType.Mp);
            return new ChooseCardParam(config.AppId, shopId, cardType, cardId);
        }
        /// <summary>
        /// 获取批量添加卡券的实体
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="code">指定的卡券code码，只能被领一次。自定义code模式的卡券必须填写，非自定义code和预存code模式的卡券不必填写。详情见： 是否自定义code码</param>
        /// <param name="openid">	指定领取者的openid，只有该用户能领取。bind_openid字段为true的卡券必须填写，bind_openid字段为false不必填写。</param>
        /// <param name="geTime">卡券在第三方系统的实际领取时间</param>
        /// <param name="outerStr">领取渠道参数，用于标识本次领取的渠道值。</param>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static CardExt GetCardExt(string cardId, string code = "", string openid = "", DateTime? geTime = null, string outerStr = "", string appid = "")
        {
            var config = OpenApi.GetConfig(appid, PlatformType.Mp);
            return new CardExt(config.AppId, cardId, code, openid, geTime, outerStr);
        }
        /// <summary>
        /// 获取批量添加卡券的参数
        /// </summary>
        /// <param name="cards"></param>
        /// <param name="appId"></param>
        /// <returns></returns>
        public static List<AddCardParam> GetAddCardParams(List<CardExtInfo> cards, string appId="")
        {
            var config = OpenApi.GetConfig(appId, PlatformType.Mp);
            var cardParams = new List<AddCardParam>();
            foreach (var extInfo in cards)
            {
                cardParams.Add(new AddCardParam
                {
                    cardId = extInfo.CardId,
                    cardExt = JsonConvert.SerializeObject(new CardExt(config.AppId, extInfo.CardId, extInfo.Code, extInfo.OpenId, extInfo.GetTime, extInfo.OuterStr))
                });
            }
            return cardParams;
        }

      

    }
}